package com.edu.juc.cas.atomic;

import com.edu.juc.cas.bean.MyNumber;

import java.util.concurrent.CountDownLatch;

public class AtomicIntegerTest {
    static final int SIZE = 50;

    public static void main(String[] args) throws InterruptedException {
        // AtomicInteger
        MyNumber myNumber = new MyNumber();
        CountDownLatch countDownLatch = new CountDownLatch(SIZE);
        for (int i = 0; i < SIZE; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    // 原子操作： atomicInteger.getAndIncrement();
                    myNumber.addI();
                }
                countDownLatch.countDown();
            }, "Thread" + i).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "::" + myNumber.get());

    }
}
